<template>
	<view>
		<view class="msg_content_bg">
			<view class="add_msg_bg">
				<!-- 发布新消息的按钮 -->
				<button class="add_btn" type="default" style="color: #a6a6a6;" @click="addMessage">发布新消息</button>
			</view>
			<msgcontent v-for="(item,index) in msgList" :key="index" :msgobj="item" @click="editMessage(index)">
			</msgcontent>
		</view>
	</view>
</template>

<script>
	import config from '../../common/config.js';
	export default {
		onShow() {
			this.getMyMessages();
		},
		onPullDownRefresh() {
			console.log('refresh');
			this.getMyMessages();

		},
		onLoad() {
			setTimeout(function() {
				console.log('start pulldown');
			}, 1000);
			uni.startPullDownRefresh();
		},
		data() {
			return {
				msgList: new Array()
			}
		},
		methods: {
			addMessage(){
				uni.navigateTo({
					url:"/pages/addmessage/addmessage"
				});
			},
			editMessage(index) {
				let msg = this.msgList[index];
				uni.navigateTo({
					url: "/pages/editmessage/editmessage?item=" + encodeURIComponent(JSON.stringify(msg))
				});
			},
			getMyMessages() {
				let mymsgURL = config.baseURL + "message/msglist";
				self = this;
				uni.request({
					url: mymsgURL,
					method: 'GET',
					header: {
						"Authorization": config.getToken()
					},
					success: (res) => {
						console.log(res.data);
						setTimeout(function() {
							uni.stopPullDownRefresh();
						}, 1000);
						if (res.data.isok == 1) {
							let filteredArray = Array.from(res.data.data.message).filter(item => item !==
								undefined);
							self.msgList = filteredArray;
							console.log(self.msgList);
						} else {
							console.log(res.data.error_code);
							if (res.data.error_code == 401) {
								uni.reLaunch({
									url: "/pages/login/login"
								})
							}
							uni.showToast({
								title: "消息获取失败",
								icon: "error"
							})
						}
					},
					fail: (res) => {
						console.log(res.errMsg.error_code);
						setTimeout(function() {
							uni.stopPullDownRefresh();
						}, 1000);
						if (res.errMsg.error_code == 401) {
							uni.reLaunch({
								url: "/pages/login/login"
							})
						}
						uni.showToast({
							title: "消息获取失败",
							icon: "error"
						})
					}
				})
			}
		}
	}
</script>

<style>
	.add_msg_bg {
		width: 690rpx;
		margin: 30rpx;
		padding-top: 10rpx;
		padding-bottom: 10rpx;
/* 		position: fixed;
		display: flex;
		z-index: 999; */
		
	}

	.add_btn {
		width: 650rpx;
		height: 80rpx;
		border-radius: 40rpx;
		font-size: 30rpx;
		border-width: 3rpx;
		border-style: dashed;
	}
</style>